package de.idgard.android.addon.ui.dialogs;

import android.app.Dialog;
import android.content.Context;
import android.text.Html;
import android.text.SpannableString;
import android.text.style.UnderlineSpan;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import de.idgard.android.R;
import de.idgard.android.addon.backend.IDGBackendService;
import de.idgard.android.addon.services.GlobalServiceRegistry;
import de.idgard.android.addon.ui.services.BrowserTabService;
import de.idgard.android.addon.ui.services.LayoutAdjustmentService;
import de.idgard.android.addon.ui.services.PropertiesService;
import de.idgard.android.addon.ui.services.impl.UIServiceRegistry;
import de.idgard.json.data.PrivacyCounter;
import de.idgard.json.transport.Receiver;

/**
 * @author pratik popat.
 * 
 */
public class IDGPrivacyCounterDialog extends Dialog {

	private Button btnClose;
	private final String COUNTER_FORMAT_STR1 = "&#160;<big><b><font color='#00244A'>";
	private final String COUNTER_FORMAT_STR2 = "</font></b></big>&#160;";

	private IDGBackendService getBackendService() {
		return GlobalServiceRegistry.instance().getBackendService();
	}

	private PropertiesService getPropertiesService() {
		return GlobalServiceRegistry.instance().getPropertiesService();
	}

	private BrowserTabService getBrowserTabService() {
		return GlobalServiceRegistry.instance().getBrowserTabService();
	}

	private LayoutAdjustmentService getLayoutAdjustmentService() {
		return UIServiceRegistry.instance().getlayouAdjustmentService();
	}

	private TextView tvCounterString, tvMore, tvReset;
	private ProgressBar pbCounter;

	public IDGPrivacyCounterDialog(Context context) {
		super(context);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.idgard_dialog_counter);

		int width = getLayoutAdjustmentService().getScreenWidth();
		width = width * 80 / 100;
		getLayoutAdjustmentService()
				.adjustDialogLayout(this, -1, width, -1, -1);

		tvMore = (TextView) findViewById(R.id.tv_counter_more);
		tvCounterString = (TextView) findViewById(R.id.tv_counterStr);
		tvReset = (TextView) findViewById(R.id.tv_counter_reset);
		btnClose = (Button) findViewById(R.id.counterCloseBtn);
		pbCounter = (ProgressBar) findViewById(R.id.pb_counter);

		formatAndSetDialogString();

		tvMore.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Log.i(this.getClass().getName(), "counter more action...");

				getBrowserTabService().addNewTab();
				GlobalServiceRegistry
						.instance()
						.getZircoToIDGardAPI()
						.navigateToUrl(
								getPropertiesService().getString(
										R.string.idgard_counter_url));
				dismiss();
			}
		});
		tvReset.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Log.i(this.getClass().getName(), "counter resetting...");
				getBackendService().resetPrivacyCounter().fire(
						new Receiver<Void>() {
							@Override
							public void onSuccess(Void response) {
								setCounter();
							}
						});
			}
		});
		btnClose.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				dismiss();
			}
		});

	}

	private void formatAndSetDialogString() {
		SpannableString moreU = new SpannableString(getPropertiesService()
				.getString(R.string.dialog_counter_more));
		moreU.setSpan(new UnderlineSpan(), 0, moreU.length(), 0);
		tvMore.setText(moreU);

		SpannableString resetU = new SpannableString(getPropertiesService()
				.getString(R.string.dialog_counter_reset));
		resetU.setSpan(new UnderlineSpan(), 0, resetU.length(), 0);
		tvReset.setText(resetU);

		setCounter();
	}

	private void setCounter() {
		pbCounter.setVisibility(View.VISIBLE);

		getBackendService().queryPrivacyCounter().fire(
				new Receiver<PrivacyCounter>() {
					@Override
					public void onSuccess(PrivacyCounter resp) {
						String countStr = COUNTER_FORMAT_STR1 + resp.getValue()
								+ COUNTER_FORMAT_STR2;
						String str1 = getPropertiesService().getString(
								R.string.dialog_counter_str1);
						String str2 = getPropertiesService().getString(
								R.string.dialog_counter_str2);
						tvCounterString.setText(Html.fromHtml(str1 + countStr
								+ str2));
						pbCounter.setVisibility(View.INVISIBLE);
					}

					@Override
					public void onFailure(int code, String response) {
						pbCounter.setVisibility(View.INVISIBLE);
						tvCounterString.setText(getPropertiesService()
								.getString(R.string.err_counter_loading));
						super.onFailure(code, response);
					}
				});
	}

}
